package 面向对象;
import java.util.Arrays;
public class 继承3应用实例
{
	public static void main(String[] args){
		ImportCosmeticManager cm=new ImportCosmeticManager();
		cm.add(new Cosmetic("大宝","国产",28));
		cm.add(new Cosmetic("迪奥","进口",698));
		cm.add(new Cosmetic("无限极","国产",128));
		cm.add(new Cosmetic("罗兰","进口",338));
		cm.printInfo();
	}
}
//继承管理类实现打印type为进口的信息
class ImportCosmeticManager extends CosmeticManager
{

	@Override
	/**
	  override 重写，子类重写父类方法,必须 方法名 参数 返回值都相同
	  		   父类private static final修饰的方法不可重写
	  overload 重载，本类中方法重载，方法名相同，返回值 参数可不同
	*/
	public void printInfo()
	{
		// TODO: Implement this method
		for(int i=0;i<count;i++){
			//判断字符串是否相等用equals()函数，==比较的是内存引用地址
			if("进口".equals(cs[i].getType())){
				cs[i].getInfo();
			}
		}
	}
	
}
//继承管理类实现按价格排序
class SortCosmeticManager extends CosmeticManager
{
	
	@Override
	public void printInfo()
	{
		// TODO: Implement this method
	    Cosmetic[] tmp=Arrays.copyOf(cs,count);
		Cosmetic c=null;
		//冒泡排序
		for (int i=0;i<tmp.length-1;i++){
			for (int j=0;j<tmp.length-i-1;j++){
				if(tmp[j].getPrice()>tmp[j+1].getPrice()){
					c=tmp[j];
					tmp[j]=tmp[j+1];
					tmp[j+1]=c;
				}
			}
		}
		for(Cosmetic cc:tmp){
			cc.getInfo();
		}
	}
	
}
//管理类
class CosmeticManager{
	protected Cosmetic[] cs=new Cosmetic[4];
	protected int count=0;
	public CosmeticManager(){}//如果不写，系统自动添加默认的
	
	public void add(Cosmetic c){
		int size=cs.length;
		if(count>=size){//实现动态数组
			int newLen=size*2;
			cs=Arrays.copyOf(cs,newLen);
		}
		cs[count++]=c;
	}
	public void printInfo(){
		for(int i=0;i<count;i++){
			cs[i].getInfo();
		}
	}
}
//化妆品类
class Cosmetic{
	private String name;//名称
	private String type;//类型 进口 国产
	private int price;//价格
	public Cosmetic(){}
	public Cosmetic(String name,String type,int price){
		this.name=name;
		this.type=type;
		this.price=price;
	}
	public void setName(String name){
		this.name=name;
	}
	public void setType(String type){
		this.type=type;
	}
	public void setPrice(int price){
		this.price=price;
	}
	public String getName(){
		return name;
	}
	public String getType(){
		return type;
	}
	public int getPrice(){
		return price;
	}
	public void getInfo(){
		System.out.println("name="+name+",type"+type+",price="+price);
	}
}
